var root = ui("$");
var nf = sm("do_Notification");
var page = sm("do_Page");
var do_GridView_1 = ui("do_GridView_1");
var deviceone = require("deviceone");
var dialog = sm("do_Dialog");
var communityUtil = require("helper/community");
var helperOpen = require("helper/open");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var bizzType = require("helper/bizzType");
var pubUrl = require("url/pub");
var communityUrl = require("url/community");
var deviceHelper = require("helper/device");
// UI
var do_ALayout_6 = ui("do_ALayout_6");
var linearLayoutRow = ui("linearLayoutRow");
var labelContent = ui("labelContent");
var do_LinearLayout_parise=ui("do_LinearLayout_parise");
// 设置数据绑定的映射关系
root.setMapping({ // 此处的Mapping只能设置在 RootView 上 ;
	"do_ALayout_6.tag" : "creatorId",
	"labelUserNick.text" : "userNick",
	"labelContent.text" : "content",
	"imageViewUserLogo.source" : "userLogo",
	"labelTime.text" : "dayMinutesNewAt",
	"do_GridView_1.items" : "imgItems",
	"do_GridView_1.tag" : "bigImgItems",
	"do_GridView_1.visible" : "hasImg",
	"do_ImageView_5.tag" : "isConcern",
	"do_ALayout_5.tag" : "id",
	"do_ALayout_10.visible":"isShowPraiseList",
	"do_LinearLayout_parise.tag" : "userList4Praise",//点赞人列表
	"labelTopType.tag" : "isTop",
	"do_LinearLayout_userName.tag" : "isOfficial"
});

root.on("dataRefreshed", function() {
	if (ui("do_ImageView_5").tag == 1) {
		ui("do_ImageView_5").source = "source://image/common/concern1.png";
	} else {
		ui("do_ImageView_5").source = "source://image/common/concern0.png";
	}
	setTopStyle();
});
page.on("showAllPraise", function(data, e) {
	showAllPraise(data.userList4Praise);
});
function showAllPraise(userList4Praise){
	var userList = userList4Praise;//点赞人列表
	var keyIds = do_LinearLayout_parise.getChildren();
	for(var i in keyIds){
		ui(keyIds[i]).remove();
	}
	if (userList.length>0) {
		var y=0;
		var i=0
		var keyrow=do_LinearLayout_parise.add("keyrow"+y, "source://view/public/praise/detailPraiseRow.ui", "");
		var keyrowll=ui(keyrow+".do_LinearLayout_row");
		var label;
		do {
			var keyl=keyrowll.add("key"+i, "source://view/public/praise/detailPraise.ui", "key"+(i-1));
		
			label=ui(keyl+".do_Label_key");
			label.text=" "+userList[i].nick;
			ui(keyl+".do_ALayout_root").tag=userList[i].id;
			
			if (keyrowll.getRect().width>do_LinearLayout_parise.width){
				y+=1;
				keyrow = do_LinearLayout_parise.add("keyrow" + y, "source://view/public/praise/detailPraiseRow.ui", "keyrow" + (y - 1));
				keyrowll = ui(keyrow + ".do_LinearLayout_row");
				// 删掉添加的label
				ui(keyl).remove();
				continue;
			}
			
			//点击搜索
			ui(keyl+".do_ALayout_root").on("touch", "", 2000, function() {
				helperOpen.openPersonal(this.tag);
			});	
			i+=1;
		} while (i < userList.length);
	}
	page.fire("refreshList");
}
/**
 * 渲染置顶样式及文字
 * @returns
 */
function setTopStyle() {
	var isTop = ui("labelTopType").tag;//是否置顶
	var isOfficial = ui("do_LinearLayout_userName").tag;//是否官方
	if(isTop != "1"){
		ui("labelTopType").visible = false;
	}else{
		ui("labelTopType").visible = true;
		if(isOfficial == "1"){
			ui("labelTopType").text ="官方";
			ui("labelTopType").fontColor = "FF7E00FF";
			ui("labelTopType").border =	"FF7E00FF,1,[5,5,5,5]";
		}else{
			ui("labelTopType").text ="置顶";
			ui("labelTopType").fontColor = "FF0080FF";
			ui("labelTopType").border =	"FF0080FF,1,[5,5,5,5]";
		}
	}
	
}
page.on("onPraiseBack", function(data, e) {
	var messageId = ui("do_ALayout_5").tag;
	httpSilence.post(communityUrl.url.message.getPraiseUserList, {"id":messageId}, function(data) {
		page.fire("praise2UpdateCommunityMessage",data);
	});
});

// 图片浏览注册事件
do_GridView_1.on("touch", function(data) {
	helperOpen.open("source://view/reuse/imgBrowser.ui", {
		imgDatas :do_GridView_1.tag,index:data
	});
})
// 访问他、她的资料
do_ALayout_6.on("touch", function(data) {
	helperOpen.openPersonal(this.tag);
});


// 点赞按钮
ui("do_ALayout_5").on("touch", function(data, e) {
	//列表页不触发点赞和评论事件，点击进入详情页
	if((ui("do_ALayout_10").visible)){
		var bizzId = ui("do_ALayout_5").tag;
		var addParams = {
			bizzId : bizzId,
			bizzType : bizzType.Level2.COMMUNITY_MESSAGE
		};
		if (ui("do_ImageView_5").tag == 1) {
			// 如果已点赞，再次点击则取消点赞
			qqHttp.post(pubUrl.url.concern.doCancle, addParams, function(data) {
				ui("do_ImageView_5").tag = 2;
				ui("do_ImageView_5").source = "source://image/common/concern0.png";
			});
		} else {
			qqHttp.post(pubUrl.url.concern.doAdd, addParams, function(data) {
				ui("do_ImageView_5").tag = 1;
				ui("do_ImageView_5").source = "source://image/common/concern1.png";
			});
		}
	}
});

/*复制文字*/
var descLayout = ui("descLayout");
descLayout.on("touch","",300,function(){
	var datas = {
		"text":	labelContent.text
	}
	var osType = deviceHelper.getOsType();
	var dialogHref = osType==="2"?"source://view/reuse/copyTextIos.ui":"source://view/reuse/copyText.ui";
	dialog.open(dialogHref, datas, function(data, e) {  
	});
});